//
//  FPLoginAndRegisterTools.m
//  ForestPack
//
//  Created by jimmy on 2018/7/25.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "FPLoginAndRegisterTools.h"
#import "sys/sysctl.h"

@implementation FPLoginAndRegisterTools

//密码
+(BOOL)checkPassword:(NSString *) password
{
    NSString *pattern = @"[0-9 | A-Z | a-z]{6,16}";
    /**
     （1）该字符串必须要含有数字
     （2）该字符串必须含有字母
     （3）6-16位
     （4）不含其它字符，如_，&等。
     */
    //    NSString *pattern = @"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,16})$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
    
}

//6位验证码
+ (BOOL)checkYzmField:(NSString *)yzmField
{
    
    NSString *pattern = @"^\\d{6}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:yzmField];
    return isMatch;
    
}
////手机号码正则
//+(BOOL) isValidateMobile:(NSString *)mobileNum
//{
//    NSString * MOBILE = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0-9])|(17[0-9]))\\d{8}$";
//    
//    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
//    
//    BOOL isMatch = [regextestmobile evaluateWithObject:mobileNum];
//    return isMatch;
//    
//}

+(NSString*)changeNullToBlank:(NSString*)str
{
    if (!str) {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",str];
}

+(NSString*)getToken
{
    NSUserDefaults *SaveDefaults = [NSUserDefaults standardUserDefaults];
    NSString* token = [SaveDefaults objectForKey:@"token"];
    return [FPLoginAndRegisterTools changeNullToBlank:token];
}


+ (NSString*) doDevicePlatform
{
    size_t size;
    int nR = sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *machine = (char *)malloc(size);
    nR = sysctlbyname("hw.machine", machine, &size, NULL, 0);
    NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
    free(machine);
    
    if ([platform isEqualToString:@"iPhone3,1"]||[platform isEqualToString:@"iPhone3,2"]||[platform isEqualToString:@"iPhone3,3"]) {
        
        platform = @"iPhone 4";
        
    } else if ([platform isEqualToString:@"iPhone4,1"]) {
        
        platform = @"iPhone 4S";
        
    } else if ([platform isEqualToString:@"iPhone5,1"]||[platform isEqualToString:@"iPhone5,2"]) {
        
        platform = @"iPhone 5";
        
    }else if ([platform isEqualToString:@"iPhone5,3"]||[platform isEqualToString:@"iPhone5,4"]) {
        
        platform = @"iPhone 5C";
        
    }else if ([platform isEqualToString:@"iPhone6,2"]||[platform isEqualToString:@"iPhone6,1"]) {
        
        platform = @"iPhone 5S";
        
    }
    
    return platform;
}


///13位时间戳转换为年月日24小时制
+(NSString *)changeTime:(NSString *)timeString
{
    // timeStampString 是服务器返回的13位时间戳
    NSString *timeStampString  = timeString;
    
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeStampString doubleValue] / 1000.0;
    NSDate *date  = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateString       = [formatter stringFromDate: date];
    NSLog(@"服务器返回的时间戳对应的时间是:%@",dateString);
    
    return dateString;
}

@end
